package com.lib.utils;

import android.os.Handler;
import android.view.View;

import java.util.ArrayList;

public class AutoRotateHelper {
    public static final int DIRECTION_X = 0x0;
    public static final int DIRECTION_Y = 0x1;
    public static final int DIRECTION_Z = 0x2;
    public static final int DIRECTION_XYZ = 0x3;

    private int mRotateDirection = DIRECTION_Z;

    private final int DEFAULT_TIMES = 999999999;
    private int mBackupRotateTimes = DEFAULT_TIMES;
    private int mRotateTimes = DEFAULT_TIMES;

    private final int mRotatedAngle = 10;
    private int mHaveRotatedAngle;

    private int mRotateIntervalMillis = 60;
    private boolean mAutoRotate;
    private long mStartMs;

    private Handler mH;
    private Runnable mRunnable;
    private ArrayList<View> mList;

    public AutoRotateHelper() {
        mH = new Handler();
        mList = new ArrayList<>();
        mRunnable = new MyRunnable();
    }

    public AutoRotateHelper addView(View view) {
        mList.add(view);
        return this;
    }

    public AutoRotateHelper setRotateDirection(int rotateDirection) {
        mRotateDirection = rotateDirection;
        return this;
    }

    /**
     * 旋转间隔，间隔越小，旋转越快
     *
     * @param millis 间隔时间，单位毫秒
     * @return
     */
    public AutoRotateHelper setRotateIntervalMillis(int millis) {
        if (millis < 0) {
            millis = 0;
        }
        mRotateIntervalMillis = millis;
        return this;
    }

    /**
     * 旋转次数
     *
     * @param times
     */
    public void setRotateTimes(int times) {
        mBackupRotateTimes = mRotateTimes = times;
    }

    public int getRotateIntervalMillis() {
        return mRotateIntervalMillis;
    }

    public void setAutoRotate(boolean autoRotate) {
        if (mList == null || mList.size() == 0) {
            XLog.i("need addView first");
            return;
        }
//        XLog.i("" + autoRotate);
        if (mAutoRotate != autoRotate) {
            mAutoRotate = autoRotate;
            if (mAutoRotate) {
                mStartMs = System.currentTimeMillis();
                mH.postDelayed(mRunnable, mRotateIntervalMillis);
            }
        }
    }

    public boolean isAutoRotate() {
        return mAutoRotate;
    }

    public void onDestroy() {
        mH.removeCallbacksAndMessages(null);
        mH = null;

        mList.clear();
        mList = null;

        mRunnable = null;
    }

    private class MyRunnable implements Runnable {
        @Override
        public void run() {
            mHaveRotatedAngle = (mHaveRotatedAngle + mRotatedAngle) % 360;
//            XLog.i("mHaveRotatedAngle->" + mHaveRotatedAngle);
            if (mHaveRotatedAngle == 0) {
                --mRotateTimes;
            }
            if (mRotateTimes > -1) {
                switch (mRotateDirection) {
                    case DIRECTION_X:
                        for (View view : mList) {
                            view.setRotationX(mHaveRotatedAngle);
                        }
                        break;
                    case DIRECTION_Y:
                        for (View view : mList)
                            view.setRotationY(mHaveRotatedAngle);
                        break;
                    case DIRECTION_Z:
                        for (View view : mList)
                            view.setRotation(mHaveRotatedAngle);
                        break;
                    case DIRECTION_XYZ:
                        for (View view : mList) {
                            view.setRotationX(mHaveRotatedAngle);
                            view.setRotationY(mHaveRotatedAngle);
                            view.setRotation(mHaveRotatedAngle);
                        }
                }
                long ms = System.currentTimeMillis();
                if (mAutoRotate || ms - mStartMs < 1000 || mHaveRotatedAngle != 0) {
                    mH.postDelayed(mRunnable, mRotateIntervalMillis);
                } else {
                    reset();
                }
            } else {
                reset();
            }
        }
    }

    private void reset() {
        for (View view : mList) {
            view.setRotationX(0);
            view.setRotationY(0);
            view.setRotation(0);
        }
        mAutoRotate = false;
        mHaveRotatedAngle = 0;
        mRotateTimes = mBackupRotateTimes;
    }
}